<template>
    <!-- 操作按钮区 -->
    <div class="action-buttons">
      
      <el-button type="success" @click="batchSaveDetails" class="add-button">添加</el-button>
    </div>
  
    <div>
      <!-- 查询条件部分 -->
      <!-- <div class="search-section">
        <el-form :inline="true" :model="prolist">
            <el-form-item label="设备名称">
              <el-input v-model="prolist.factory" placeholder="输入设备名称"></el-input>
            </el-form-item>
            <el-form-item label="操作时间">
              <el-date-picker v-model="prolist.transactionTime" type="date" placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="设备编号">
              <el-input v-model="prolist.workOrderNo" placeholder="输入设备编号"></el-input>
            </el-form-item>
            <el-form-item label="设备状态">
              <el-input v-model="prolist.finishedProductBatch" placeholder="输入设备状态"></el-input>
            </el-form-item>
            <el-form-item label="操作人">
              <el-input v-model="prolist.productionLine" placeholder="输入操作人"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="ShowMaterials" class="search-button">查询</el-button>
            </el-form-item>
          </el-form>
      </div> -->
      <div class="page-title">
        <h2>专检申请列表</h2>
      </div>
      <div class="table-container">
        <el-table :data="planList" stripe ref="tableRef" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection"></el-table-column>
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="taskNumber" label="任务号"></el-table-column>
          <el-table-column prop="taskType" label="任务类型"></el-table-column>
          <el-table-column prop="workSection" label="工段"></el-table-column>
          <el-table-column prop="productionLine" label="产线"></el-table-column>
          <el-table-column prop="team" label="班组"></el-table-column>
          <el-table-column prop="shift" label="班次"></el-table-column>
          <el-table-column prop="workOrderNumber" label="工单号"></el-table-column>
          <el-table-column prop="productCode" label="产品编码"></el-table-column>
          <el-table-column prop="productName" label="产品名称"></el-table-column>
          <el-table-column prop="batchNumber" label="批次号"></el-table-column>
          <el-table-column prop="quantity" label="数量"></el-table-column>
          <el-table-column prop="unit" label="单位"></el-table-column>
          <el-table-column prop="samplingPoint" label="采样点"></el-table-column>
          <el-table-column prop="controlBatchNumber" label="控制批次号"></el-table-column>
          <el-table-column prop="preFermentationTankNumber" label="前酵罐号"></el-table-column>
          <el-table-column prop="postFermentationTankNumber" label="后酵罐号"></el-table-column>
          <el-table-column prop="sakeTankNumber" label="清酒罐号"></el-table-column>
          <el-table-column prop="fullTankTime" label="满罐时间">
            <template #default="scope">
              {{ formatDate(scope.row.fullTankTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="right" label="操作">
            <template #default="scope">
              <el-button type="text" @click="GetBYIdProductionPlanning(scope.row.id)" class="edit-button">编辑</el-button>
              <el-button type="text" @click="DeleteProductionPlanning(scope.row.id)" class="delete-button">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div style="margin-top: 20px">
        <el-pagination
          v-model:current-page="page.pageindex"
          v-model:page-size="page.pageSize"
          :page-sizes="[4, 8, 15, 20]"
          :background="true"
          prev-text="上一页"
          next-text="下一页"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total.totalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { Materialread, Materialwrite } from '@/axios/axiosHelper';
  import { ElMessage, ElMessageBox } from 'element-plus';
  import router from '@/router';
  import axios from 'axios';
  
  // 定义搜索表单数据
  const prolist = ref({
    
  });
  
  // 定义分页总数
  const total = ref({
    totalCount: 0,
    totalPage: 0,
  });
  
  // 定义分页对象
  const page = ref({
    pageindex: 1,
    pageSize: 15,
  });
  
  const formatDate = (dateStr) => {
    const date = new Date(dateStr);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    const second = String(date.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  };
  
  // 模拟生产计划列表数据
  const planList = ref([
    {
        "id": 0,
        "taskNumber": "",
        "taskType": "",
        "workSection": "",
        "productionLine": "",
        "team": "",
        "shift": "",
        "workOrderNumber": "",
        "productCode": "",
        "productName": "",
        "batchNumber": "",
        "quantity": 0,
        "unit": "",
        "samplingPoint": "",
        "controlBatchNumber": "",
        "preFermentationTankNumber": "",
        "postFermentationTankNumber": "",
        "sakeTankNumber": "",
        "fullTankTime": "",
        "isDel": false
    }
  ]);
  
  // 表格引用
  const tableRef = ref(null);
  
  // 全选状态
  const allChecked = ref(false);
  
  // 选中的 ID 列表
  const checkedIds = ref([]);
  
  const ShowMaterials = () => {
    axios.get("https://localhost:7238/api/ProductionTask/GetProductionTask", {
      params: {
        pageindex: page.value.pageindex,
        pageSize: page.value.pageSize,

      }
    }).then((res) => {
      planList.value = res.data.data.list;
      console.log("专检申请显示数据", res.data.data);
  
      total.value.totalCount = res.data.data.totalCount;
      total.value.totalPage = res.data.data.totalPage;
      // 重置全选状态和选中的 ID 列表
      allChecked.value = false;
      checkedIds.value = [];
      console.log(res.data.data);
    }).catch((error) => {
      console.error('接口请求失败:', error);
      ElMessage.error('数据获取失败，请稍后重试');
    });
  };
  
  // 编辑跳转修改
  const GetBYIdProductionPlanning = (id) => {
    router.push({
      name: 'UpdateProductionTask',
      query: {
        id: id
      }
    }).catch((error) => {
      console.error('路由跳转错误:', error);
      ElMessage.error('跳转失败，请检查');
    });
    console.log(id);
  };
  
  const batchSaveDetails = () => {
    router.push({
      name: 'AddProductionTask',
    });
  };
  
  
  const DeleteProductionPlanning = async (id) => {
    try {
      const confirmResult = await ElMessageBox.confirm(
        '确认删除该条记录？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      );
      if (confirmResult === 'confirm') {
        const response = await axios.post(
          `https://localhost:7229/api/ProductionTask/DelProductionTask`,
          { id: id.toString() }, // 修改这里，将参数放在请求体中
          {
            params: {id: id.toString()} // 这里保持为空，因为参数已经在请求体中
          }
        );
        if (response.data.code === 200) {
          ElMessage.success('专检申请删除成功');
          ShowMaterials(); // 调用 ShowMaterials 方法刷新数据
        } else {
          ElMessage.error(`专检申请删除失败：${response.data.message}`);
        }
      }
    } catch (error) {
      if (error!== 'cancel') {
        console.error('专检申请删除错误:', error);
        ElMessage.error('专检申请删除失败，请检查网络或联系管理员');
      }
    }
  };
  const handleSizeChange = (val) => {
    page.value.pageSize = val;
    ShowMaterials();
  };
  
  const handleCurrentChange = (val) => {
    page.value.pageindex = val;
    ShowMaterials();
  };
  
  onMounted(() => {
    ShowMaterials();
  });
  </script>
  
  <style scoped>
  body {
    font-family: 'Arial', sans-serif;
    background-color: #f0f2f5;
    margin: 0;
    padding: 0;
  }
  
  .action-buttons {
    display: flex;
    justify-content: center;
    padding: 20px;
  }
  
  .import-button,
  .add-button,
  .search-button {
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;
  }
  
  .import-button {
    border: 1px solid #ccc;
    background-color: white;
    color: #333;
    margin-right: 10px;
  }
  
  .add-button {
    border: none;
    background-color: #42b983;
    color: white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  .search-button {
    border: 1px solid #42b983;
    background-color: white;
    color: #42b983;
  }
  
  .search-section {
    background-color: white;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
    margin: 0 20px 20px;
  }
  
  .page-title {
    text-align: center;
    color: #2c3e50;
    margin-bottom: 20px;
  }
  
  .table-container {
    background-color: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
    margin: 0 20px;
    max-height: 400px;
    overflow-y: auto;
  }
  
  .el-table {
    width: 100%;
    border-collapse: collapse;
  }
  
  .el-table__header-wrapper {
    background-color: #f4f4f4;
  }
  
  .el-table__header th {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #ccc;
    color: #333;
  }
  
  .el-table__body td {
    padding: 10px;
    border-bottom: 1px solid #eee;
    color: #666;
  }
  
  .edit-button,
  .delete-button {
    color: #42b983;
    padding: 5px 10px;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease, color 0.3s ease;
  }
  
  .edit-button:hover {
    background-color: #f0f9eb;
  }
  
  .delete-button:hover {
    color: red;
  }
  
  .el-pagination {
    display: flex;
    justify-content: center;
    margin: 20px 0;
  }
  </style>